package com.sgu6.sgu6_machine.ui.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.sgu6.sgu6_machine.R;


/**
 * 自定义组件：购买数量，带减少增加按钮
 */
public class AmountView extends LinearLayout implements View.OnClickListener, TextWatcher {

    private static final String TAG = "AmountView";
    private int curValue = 1; //当前值
    private int maxValue = Integer.MAX_VALUE; //最大值
    private int ratio = 1; //调整系数

    private OnAmountChangeListener mListener;

    private EditText etAmount;

    public AmountView(Context context) {
        this(context, null);
    }

    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.view_amount, this);
        etAmount = findViewById(R.id.etAmount);
        Button btnDecrease = findViewById(R.id.btnDecrease);
        Button btnIncrease = findViewById(R.id.btnIncrease);
        btnDecrease.setOnClickListener(this);
        btnIncrease.setOnClickListener(this);
        etAmount.addTextChangedListener(this);

        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView);
        int btnWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnWidth, LayoutParams.WRAP_CONTENT);
        int tvWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvWidth, 100);
        int tvTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvTextSize, 0);
        int btnTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnTextSize, 0);
        obtainStyledAttributes.recycle();

        LayoutParams btnParams = new LayoutParams(btnWidth, LayoutParams.MATCH_PARENT);
        btnDecrease.setLayoutParams(btnParams);
        btnIncrease.setLayoutParams(btnParams);
        if (btnTextSize != 0) {
            btnDecrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize);
            btnIncrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize);
        }

        LayoutParams textParams = new LayoutParams(tvWidth, LayoutParams.MATCH_PARENT);
        etAmount.setLayoutParams(textParams);
        if (tvTextSize != 0) {
            etAmount.setTextSize(tvTextSize);
        }
    }

    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.mListener = onAmountChangeListener;
    }

    public void setAmountMaxValue(int value) {
        this.maxValue = value;
    }

    public void setAmountRatio(int ratio) {
        this.ratio = ratio;
    }

    public void setDefaultValue(int defaultValue) {
        curValue = defaultValue;
        etAmount.setText(String.valueOf(defaultValue*ratio));
        invalidate();
    }

    public int getValue() {
        return curValue * ratio;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnDecrease:
                if (curValue > 1) {
                    curValue--;
                    etAmount.setText(String.valueOf(curValue * ratio));
                }
                break;
            case R.id.btnIncrease:
                if (curValue < maxValue) {
                    curValue++;
                    etAmount.setText(String.valueOf(curValue * ratio));
                }
                break;
        }

        etAmount.clearFocus();

        if (mListener != null) {
            mListener.onAmountChange(this, curValue);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().isEmpty())
            return;
        curValue = Integer.valueOf(s.toString());
        if (curValue > maxValue) {
            etAmount.setText(String.valueOf(maxValue * ratio));
            return;
        }
        curValue = Integer.parseInt(s.toString()) / ratio;

        if (mListener != null) {
            mListener.onAmountChange(this, curValue);
        }
    }


    public interface OnAmountChangeListener {
        void onAmountChange(View view, int curValue);
    }

}
